package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.formatter.DataTemplateFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import org.elasticsearch.action.search.SearchTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-07-27 10:00
 **/
public class PeopleNews {
	private static final Logger log = LoggerFactory.getLogger(PeopleNews.class);

	public void mainMethod() {
		List<String> entry = getEntry();
		for(String urlEntey:entry){
			HttpResponse response = null;
			try {
				response = HttpRequest.get(urlEntey).
					header("accept-encoding", "gzip, deflate, br")
					.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
					.setConnectionTimeout(15000)
					.execute();
			} catch (Exception e) {
				continue;
			}
			String resp = response.body();
			//获取子链接
			List<String> listContent = getContentUrl(resp);
			Set set = new HashSet();
			listContent.forEach(e->{
				set.add(e);
			});
			//爬取具体内容
			try {
				log.info("人民网执行爬虫{}",set);
				getContent(listContent);
			} catch (Exception e) {
				continue;
			}
		}
	}

	private static List<String> getContentUrl(String content) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String[] ss = sdf.format(new Date()).split("-");
		String year = ss[0];
		String mon = ss[1];
		String day = ss[2];

		List<String> res = new ArrayList<>();
		String pattern = "/n1/" + year + "/" + mon + day + "/.*?html";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add("http://military.people.com.cn"+m.group());
		}
		return res;
	}

	private static void getContent(List<String> listContent) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		for (String url : listContent) {
			HttpResponse response = null;
			try {
				response = HttpRequest.get(url)
					.setConnectionTimeout(15000)
					.execute();
			} catch (Exception e) {
				continue;
			}
			String doc = response.body();
			JXDocument jxDocument = new JXDocument(doc);
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1/text()").get(0);
				if (title.equals("")) {
					title = (String) jxDocument.sel("//h1/text()").get(1);
				}
			} catch (Exception e) {
				continue;
			}
			StringBuffer content = new StringBuffer();
			jxDocument.sel("//div[@class='content clear clearfix']//p/text()").forEach(s -> {
				content.append(s+"$$$");
			});
			if (content.toString().equals("")) {
				jxDocument.sel("//div[@class='rm_txt_con cf']//p/text()").forEach(s -> {
					content.append(s+"$$$");
				});
			}
			if (content.toString().equals("")) {
				jxDocument.sel("//div[@class='artDet']//p/text()").forEach(s -> {
					content.append(s+"$$$");
				});
			}

			List<Object> imgs = new ArrayList<>();
			Document document = Jsoup.parse(doc);
			DataTemplateFormatter dataTemplateFormatter = new DataTemplateFormatter();

			try {
				String pic = "http://pic.people.com.cn" + document.getElementsByClass("pic_data").get(0).getElementsByTag("img").toString().replaceAll("<img.*?\"|\".*?>", "");
				imgs.add(pic);
				Elements picElements = document.getElementsByClass("pic_data").get(0).getElementsByClass("page_n").get(0).getElementsByTag("a");
				String pic1;
				for (int i = 1; i < picElements.size() - 1; i++) {
					String html = picElements.get(i).toString().replaceAll("<a.*?\"", "http://pic.people.com.cn").replaceAll("\">.*?>", "");
					HttpResponse response1 = HttpRequest.get(html)
						.header("accept-encoding", "gzip, deflate, br")
						.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
						.setConnectionTimeout(15000)
						.execute();
					String doc1 = response1.body();
					Document document1 = Jsoup.parse(doc1);
					pic1 = "http://pic.people.com.cn" + document1.getElementsByClass("pic_data").get(0).getElementsByTag("img").toString().replaceAll("<img.*?\"|\".*?>", "");
					imgs.add(pic1);
				}
			} catch (Exception e) {
			}
			try {
				if (imgs.size() == 0) {
					jxDocument.sel("//div[@class='rm_txt_con cf']//img/@src").forEach(s -> {
						if (!s.toString().contains("share.png") && !s.toString().contains("http")) {
							imgs.add(url.replaceAll("/n1.*?html", s.toString()));
						} else if (!s.toString().contains("share.png") && s.toString().contains("http")) {
							imgs.add(s.toString());
						}
					});
				}
			} catch (Exception e) {
			}
			Date date = null;
			try {
				date = DateUtil.parse(document.getElementsByClass("col-1-1").get(0).toString().split("\n")[1].toString().replaceAll("\\|.*?：", "").replaceAll("日", "日 ").trim());
			} catch (Exception e) {
				try {
					date = DateUtil.parse(document.getElementsByClass("page_c").get(0).getElementsByClass("fr").get(0).toString().split("\n")[2].replaceAll("<a.*?;&nbsp;", "").replaceAll("日", "日 ").trim());
				} catch (Exception exception) {
					try {
						date = DateUtil.parse(document.getElementsByClass("sou").get(0).toString().replaceAll("<p.*?>|&.*?</p>", "").replaceAll("日", "日 ").trim());
					} catch (Exception ex) {
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						String s = sdf.format(new Date());
						date = dataTemplateFormatter.format(s + " 00:00  ");
					}
				}

			}

			if (content.length() > 5000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || imgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), imgs, url, date, "http://www.people.com.cn/", "人民网军事");
				log.info(Thread.currentThread().getName() + "-----------------人民网军事----------------");
			} catch (Exception e) {
				continue;
			}
		}
	}

	public static List<String> getEntry() {
		List<String> listEntry = new ArrayList<>();
		listEntry.add("http://military.people.com.cn/");
		listEntry.add("http://military.people.com.cn/GB/172467/index.html");
		listEntry.add("http://military.people.com.cn/GB/1077/index.html");
		return listEntry;
	}

}
